import 'dart:async';

import 'package:flutter_base/common/entitys/entitys.dart';
import 'package:flutter_base/common/utils/utils.dart';
import 'package:flutter_base/common/values/values.dart';
import 'package:flutter_base/global.dart';
import 'package:flutter/material.dart';



/// 检查是否有 token
bool isAuthenticated()  {
  var profileJSON = StorageUtil().getJSON(STORAGE_USER_PROFILE_KEY);

  if(profileJSON != null){
    Global.profile  = UserResponseEntity.fromJson(profileJSON);
    Global.profile.users!.userGuid = UserResponseEntity.fromJson(profileJSON).result!.userCode;
    Global.profile.users!.deptCode =
        UserResponseEntity.fromJson(profileJSON).dept![0].department!.deptCode ?? '';
    Global.profile.users!.deptName =
        UserResponseEntity.fromJson(profileJSON).dept![0].department!.deptName ?? '';
    Global.userCode= Global.profile.result!.userAccount;
    Global.userName= Global.profile.users!.userName;
    Global.userMobile= Global.profile.passport!.mobile;
    for(Dept item in Global.profile.dept!){
      if(item.deptMember!.isDefault == "Y"){
        Global.deptCode = item.deptMember!.deptCode;
        Global.deptName = item.deptMember!.deptName;
      }
    }
  }
  return profileJSON != null ? true : false;
}

/// 删除缓存 token
Future deleteAuthentication() async {
  await StorageUtil().remove(STORAGE_USER_PROFILE_KEY);
  // Global.profile = null;

}

/// 重新登录
Future goLoginPage(BuildContext context) async {
  await deleteAuthentication();
  Navigator.pushNamedAndRemoveUntil(
      context, "/sign-in", (Route<dynamic> route) => false);
}
